define([
    'jquery',
    'template',
    'utils',
], function($,template) {
     class Header{
         constructor(){
            this.loadHTML().then(()=>{
                this.search()
                this.calcshopcar()
            })
           
         }
         loadHTML(){
            return new Promise((resolve)=>{
                $('.header').load('/html/module/header.html',()=>{
                    resolve()
                })
            })
            
         }
         search(){
            $('#searchinput').on('keyup',function(){
                $.getJSON("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?cb=?",{ wd: this.value},(resp)=>{
                const {s}=resp
                // console.log(s)
                $('#olsearch').html(template('search',{list:s}))
                $('#olsearch').show()
                })
                
            })
            $('#olsearch').on('click','li',function(){
                $('#searchinput').val($(this).text())
                $('#olsearch').hide()
                                
            })
            $(document).on('click',function(){
                $('#olsearch').hide()
            })
            
         }
         calcshopcar(){
            let sum=0;
            let cart=localStorage.getItem('cart');
            if(cart){
                cart=JSON.parse(cart)
                sum=cart.reduce((total,shop)=>{
                    return total+Number(shop.num)
                },0)
            }
            $('#shop span').html(sum)
            $('#car').html(sum)
         }
        
     }
     return new Header()
});